package com.lilinling.yinzhi.demand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.lilinling.yinzhi.demand.vo.AddDemandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.Query;

import com.lilinling.yinzhi.demand.dao.DemandDao;
import com.lilinling.yinzhi.demand.entity.DemandEntity;
import com.lilinling.yinzhi.demand.service.DemandService;


@Service("demandService")
public class DemandServiceImpl extends ServiceImpl<DemandDao, DemandEntity> implements DemandService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DemandEntity> page = this.page(
                new Query<DemandEntity>().getPage(params),
                new QueryWrapper<DemandEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addDemand(Long userId, AddDemandVo addDemandVo) {

        DemandEntity demandEntity = new DemandEntity();
        demandEntity.setUserId(userId);
        demandEntity.setDemTitle(addDemandVo.getDemTitle());
        demandEntity.setDemDesc(addDemandVo.getDemDesc());
        demandEntity.setPrice(addDemandVo.getPrice());
        demandEntity.setIssueTime(new Date());
        demandEntity.setStatus(1);

        baseMapper.insert(demandEntity);

    }

    @Override
    public AddDemandVo getAddDemandVoByDemandId(Long demandId) {
        DemandEntity demandEntity = baseMapper.selectById(demandId);
        AddDemandVo addDemandVo = new AddDemandVo();
        BeanUtil.copyProperties(demandEntity,addDemandVo);
        return addDemandVo;
    }



    @Override
    public List<DemandEntity> getUserDemand(Long userId) {
        List<DemandEntity> list = this.list(new QueryWrapper<DemandEntity>().eq("user_id", userId));
        List<DemandEntity> collect = list.stream().filter(item -> item.getStatus()==1).collect(Collectors.toList());
        return collect;
    }
}